% araim

HestenesJacobiComputation($rb_1$ : block of sparse vectors, $rb_2$ : block of sparse vectors, 
$G[R][R]$, $P[R][R]$)

%length(rb_{1, 1}) - number of tuples in row 1 of rb_1
%length(rb_1) - number of vectors in rb_1

%j, k are original indices of rows r_j and r_k
%P_{R, R} - matrix of (c, s) values to compute
%G_{R, R} - matrix of g values to compute
%J_{R, R} - matrix of (j_1, j_2) indices, to track state of pairwise comparisons
%\Theta_1_{R} - vector of norms of rb_1
%\Theta_2_{R} - vector of norms of rb_2

\begin{algorithmic}
\STATE $J[R][R] = (1,1)$
\REPEAT
  \STATE $finished \gets true$

  \FOR{each $r_j \in rb_1$}
    \FOR{each $r_k \in rb_2$}
      \IF{$r_j < r_k$}
        \STATE $idx_1, idx_2 \gets J[j \mod R][k \mod R]$
        \IF{$idx_1 \leq length_j$ and $idx_2 \leq length_k$}
          \STATE $finished \gets false$
                  \IF {$i_{1, idx_1} < i_{2, idx_2}$}
                    \STATE $idx_1 \gets idx_1 + 1$
                  \ELSIF {$i_{1, idx_1} > i_{2, idx_2}$}
                    \STATE $idx_2 \gets idx_2 + 1$
                  \ELSE
                    \STATE $temp_1 = v_{1, idx_1}$
                    \STATE $temp_2 = v_{2, idx_2}$
                    \STATE $c, s \gets P[j \mod R][k \mod R]$
                    \STATE $v_{1, idx_1} = c \cdot temp_1 - s \cdot temp_2$
                    \STATE $v_{2, idx_2} = c \cdot temp_1 + s \cdot temp_2$
                  \ENDIF
                  \STATE $J[j \mod R][k \mod R] \gets idx_1, idx_2$
        \ENDIF
      \ENDIF
    \ENDFOR
  \ENDFOR
\UNTIL{$finished = true$}

\end{algorithmic}

